<?php
/**
 * @package		简易CMS
 * @subpackage	获取最新的SVN版本号
 * @version		$Id: block_flashtag.php 21 2012-06-11 12:08:11Z htmambo@gmail.com $
 * @author		Hoping
 * @copyright	Copyright (C) 2010 Hoping Software Studio.
 */

if (!defined('IN_JYCMS')) {
	exit('Access Denied');
}

class block_flashtag {

	var $setting = array();

	function block_flashtag() {
		$this->setting = array(
		);
	}

	function name() {
		return lang('blockclass', 'blockclass_flashtag_script_flashtag');
	}

	function blockclass() {
		return array('flashtag', lang('blockclass', 'blockclass_portal_flashtag'));
	}

	function fields() {
		return array(
		);
	}

	function fieldsconvert() {
		return array();
	}

	function getsetting() {
		global $_G;
		$settings = $this->setting;
		return $settings;
	}

	function cookparameter($parameter) {
		return $parameter;
	}

	function getdata($style, $parameter) {
		global $_G;
		$speed = isset($parameter['speed']) ? intval($parameter['speed']) : 100;
		if(!$speed) $speed = 100;
		$width = isset($parameter['width']) ? intval($parameter['width']) : 290;
		if(!$width) $width = 290;
		$height = isset($parameter['height']) ? intval($parameter['height']) : 240;
		if(!$height) $height = 240;
		$items = isset($parameter['items']) ? intval($parameter['items']) : 100;
		if (!$items)
			$items = 100;

		$toptags = '';
		DB::query("SELECT * FROM #__common_tagslist ORDER BY allnum desc limit $items");
		$times = $min = $max = 0;
		$min = 1;
		$tags = array();
		while ($row = DB::fetch_array()) {
			$times+=$row['allnum'];
			$min = min($min, $row['allnum']);
			$max = max($max, $row['allnum']);
			$tags[] = $row;
		}
		$xmlfile = ROOT_PATH.'/data/tmp/tagcloud.xml';
		$xmlcontent = '<?xml version="1.0" encoding="UTF-8"?><root>';
		if ($tags) {
			$per = intval(($max - $min) / 10);
			if (!$per)
				$per = 1;
			foreach ($tags as $v) {
				$link = 'index.php?option=misc&task=tag&tag=' . urlencode($v['tagname']);
				$fs = intval(($v['allnum'] - $min) / $per) * 10;
				$xmlcontent .= '<a href="'.$link.'" type="text" detail="'.$v['tagname'].'" weight="'.$fs.'" />';
			}
		}
		$xmlcontent .= '</root>';
		if ($_G['setting']['rewritestatus']) {
			if ($_G['setting']['rewritestatus'] && !defined('IN_MODCP') && !defined('IN_ADMINCP')) {
				$xmlcontent = preg_replace('/href="([a-z]+)\.php/e', 'readdhost("\\1")', $xmlcontent);
				$array = rewritedata();
				$xmlcontent = preg_replace($array['search'], $array['replace'], $xmlcontent);
			}
		}
		$xmlcontent = str_replace('<a href=', '<item link=', $xmlcontent);
		cmsFS::write($xmlfile, $xmlcontent);
		$html = '';
		if ($tags) {
			if(!isset($_G['__flashtag__'])){
				$_G['__flashtag__'] = true;
				//@todo 如果本标签被多次调用，那么，此JS就需要再次引入了
				$html = '<script type="text/javascript" src="' . $_G['siteurl'] . 'static/js/swf.js"></script>';
			} else {
				$html = '';
			}
			$rstr = random(6);
			$html .='
	<div class="bm" id="flashtag'.$rstr.'"><p></p></div>
	<script type="text/javascript">
                       baidu.swf.create(
                            {
                                \'id\'                            : "TrendAnalyser",
                                \'width\'                         : "'.$width.'",
                                \'height\'                        : "'.$height.'",
                                \'ver\'                           : "9.0.0",
                                \'errorMessage\'                  : "请下载最新的Flash播放器！",
                                \'url\'                           : "' . $_G['siteurl'] . 'static/image/tagCloud.swf",
                                \'vars\'                          : "xml='.$_G['siteurl'].'data/tmp/tagcloud.xml",
                                \'bgcolor\'                       : "#FFFFFF",
                                \'wmode\'                         : "transparent",
                                \'allowscriptaccess\'             : "always"
                            },
                            "flashtag'.$rstr.'");
	</script>';
		}
		return array('html' => $html, 'data' => $tags);
	}
}
